package com.grandtech.insurance.common.db.utils;


import com.mapbox.geojson.Feature;
import org.json.JSONObject;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Map;

public final class MapUtil {

    private MapUtil() {

    }

    public static Criteria map2SimpleCriteria(Object o, Map<String, Object> map) {
        if (map == null) {
            return null;
        }
        Criteria criteria = null;
        Object val;
        for (String key : map.keySet()) {
            val = ReflectUtil.getValByNumberVar(o, key);
            if (criteria == null) {
                criteria = Criteria.where(key).is(val);
            } else {
                criteria.and(key).is(val);
            }
        }
        return criteria;
    }

    public static Query map2SimpleQuery(Object o, Map<String, Object> map) {
        Criteria criteria = map2SimpleCriteria(o, map);
        if (criteria == null) {
            return null;
        }
        return new Query(criteria);
    }

    public static Feature map2Feature(Map<String, String> featureMap) {
        JSONObject feature = new JSONObject();
        JSONObject properties = new JSONObject();
        try {
            String geoJson = featureMap.get("shape").toString();
            feature.put("type", "Feature");
            feature.put("properties", properties);
            Object val;
            for (Object key : featureMap.keySet()) {
                if (key.toString().toLowerCase().contains("shape") || key.toString().toLowerCase().contains("geom")) {
                    feature.put("geometry", new JSONObject(geoJson));
                    continue;
                }
                val = featureMap.get(key);
                if (val == null) {
                    properties.put(key.toString(), JSONObject.NULL);
                    continue;
                }
                if (val instanceof String) {
                    properties.put(key.toString(), val.toString());
                }
                if (val instanceof Number) {
                    properties.put(key.toString(), val);
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
        String json = feature.toString();
        return Feature.fromJson(json);
    }

    public static Feature map2Feature2(Map<String, Object> featureMap) {
        JSONObject feature = new JSONObject();
        JSONObject properties = new JSONObject();
        try {
            String geoJson = featureMap.get("shape").toString();
            feature.put("type", "Feature");
            feature.put("properties", properties);
            Object val;
            for (Object key : featureMap.keySet()) {
                if (key.toString().toLowerCase().contains("shape") || key.toString().toLowerCase().contains("geom")) {
                    feature.put("geometry", new JSONObject(geoJson));
                    continue;
                }
                val = featureMap.get(key);
                if (val == null) {
                    properties.put(key.toString(), JSONObject.NULL);
                    continue;
                }
                properties.put(key.toString(), val);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
        String json = feature.toString();
        return Feature.fromJson(json);
    }
}
